<template>
  <div>
    <el-dialog  v-if="visible" :destroy-on-close="true" :lock-scroll="false" title="导入信息" :visible="visible" width="750px" @update:visible="cancel" >
      <el-form  style="margin-top: 20px"  :model="form" ref="form" >
        <el-form-item  prop="file">
          <template>
            <el-upload
              :http-request="upload"
              :show-file-list="false"
              accept=".xls,.xlsx,.docx,application/msword,application/vnd.ms-excel,application/pdf"
              action="#"
              class="upload-demo"
            >
              <el-button size="small" type="primary" :loading="loading">上传</el-button>
            </el-upload>
          </template>
        </el-form-item>

      </el-form>
      <div class="el-upload__tip" style="margin-top: 10px">只能上传excel/xlsx文件，且不超过10m</div>
      <div slot="footer">
        <el-button  type="primary" @click="cancel(false)">取消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>

export default {
  props: {
    type:String,
    // 弹窗是否打开
    visible: Boolean,
  },
  data() {
    return {
      model:'#',
      fileList: [],
      loading: false,
      /*表单验证*/
      form:{},
    }
  },
  mounted() {
  },
  methods: {
    uploadHandleChange(file) {
      if (file.status === "error") {
        this.$message.error("上传失败");
      }
    },
    upload(file) {
      this.loading=true
      let url
      if(this.type==='customer'){
        url='/import/customer'
      }else if(this.type==='product'){
        url='/import/product'
      }
      let formData=new FormData
      formData.append('attachment',file.file)
      url?this.$http.post(url, formData).then(res => {
        if (res.data.code === 0) {
          let msg = res.data.msg
          if (this.type === 'product') {
            msg = msg + ", 导入数量: " + res.data.data
          }
          this.loading=false
          this.$message.success(msg)
          this.$emit('done')
        } else {
          this.loading=false
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.loading=false
        this.$message.error(e.message);
      }):false
    },
    delAttachment() {
      this.fileList = []
      this.form.attachment = ''
    },
    /*提交完成*/
    submit(formName){
      this.$refs[formName].validate((valid) => {
        if(valid){
          this.$emit('done',this.form.attachment)
        }
      });
    },
    cancel(value){
      Object.assign(this.$data, this.$options.data())
      this.$emit('update:visible', value);
    },
  }
}
</script>

<style lang="scss" scoped>
.el-date-editor{
  width: 100%;
}
</style>
